<?php
/**
 * Created by Adon.
 * User: Adon
 * Date: 2017/8/18
 * Time: 14:17
 */

namespace app\modules\mch\controllers;
use yii\web\UploadedFile;

use app\modules\mch\models\Model;
use app\models\Station;
use  app\models\User;
use yii\data\Pagination;
class StationController extends Controller
{
//路线
    public function actionIndex($cat_id = 1,$page=1)
    {
        $query = Station::find()->alias('a')->where(['a.is_delete' => 1]);
        $count = $query->count();

        $pagination = new Pagination(['totalCount' => $count, 'page' =>$page-1]);
;
        $list = $query
            ->leftJoin(['b' => User::tableName()], 'a.user_id=b.id')
            ->select('b.nickname,a.id,a.name,a.sort,a.create_time')
            ->orderBy('a.create_time DESC')
            ->asArray()
            ->all();


        return $this->render('index', ['row_count'=>$count,'pagination'=>$pagination,'list'=>$list,'cat_id' => $cat_id]);
    }
  //路线编辑/修改
    public function actionEdit($cat_id,$id=null){

        $model = Station::findOne(['id' => $id]);
        $nickname=User::findOne(['id' => $model->user_id]);
        $user=User::find()->where(['is_delete'=>0])->select('id,nickname')->all();
        if (\Yii::$app->request->isPost) {
            $user_id = \Yii::$app->request->post('user_id');
            $name = \Yii::$app->request->post('name');
            $lng = \Yii::$app->request->post('lng');
            $lat = \Yii::$app->request->post('lat');
            $sort = \Yii::$app->request->post('sort');
            if ($cat_id == 2) {
                $up = \Yii::$app->db->createCommand()->update('cshopmall_station', ['user_id' => $user_id, 'name' => $name, 'lng' => $lng, 'lat' => $lat, 'sort' => $sort], "id = {$id}")->execute();
                if ($up > 0) {
                    return [
                        'code' => 0,
                        'msg' => '保存成功',
                        'data' => ['cat_id' => $cat_id]
                    ];
                }

            }else{
                $add= \Yii::$app->db->createCommand('INSERT INTO `cshopmall_station` (`user_id`,`name`,`lng`,`lat`,`sort`,`create_time`,`is_delete`) VALUES (:user_id,:name,:lng,:lat,:sort,:create_time,:is_delete)', [
                    ':user_id' =>$user_id,
                    ':name'=>$name,
                    ':lng'=>$lng,
                    ':lat'=>$lat,
                    ':sort'=>$sort,
                    ':create_time'=>time(),
                    ':is_delete'=>1,
                ]  )->execute();
                if($add>0){
                    return [
                        'code' => 0,
                        'msg' => '保存成功',
                        'data'=>['cat_id'=>$cat_id]
                    ];
                }            }
        }
        return $this->render('edit', ['model'=>$model,'nickname'=>$nickname,'user'=>$user,'cat_id' => $cat_id]);
    }

    //删除路线
    public function actionDel($id){
        $model = Station::findOne([
            'id' => $id,
        ]);
        if ($model) {
            if($model['is_delete'] = 1){
                $update=\Yii::$app->db->createCommand()->update('cshopmall_station', ['is_delete' => 2], "id = {$id}")->execute();
                if($update>0){
                    return [
                        'code' => 0,
                        'msg' => '删除成功',
                    ];
                }
            }

        }
    }


}